package team.away.android.fubway;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.ITextureAtlas.ITextureAtlasStateListener;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.widget.Toast;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class Fubway extends BaseGameActivity  implements IOnSceneTouchListener {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	
	
	private int mBitmapTextureAtlasID = 0;

	private BitmapTextureAtlas mWelcomeBitmapTextureAtlas;
	private TextureRegion mWelcomeTextureRegion;
	
	private BitmapTextureAtlas mMapBitmapTextureAtlas;
	private TextureRegion mMapTextureRegion;
	protected BitmapTextureAtlas mTextTexture;
	protected IEntity mText;
	protected ChangeableText mChangeableText;
	private StationLocation mLocation;
	private Sprite mStartSprite;
	private Sprite mTargetSprite;
	private int mStartStationId;
	private int mTargetStationId;
	private BitmapTextureAtlas mEnterBitmapTextureAtlas;
	private TextureRegion mEnterTextureRegion;
	
	

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		InitWelcome();
		InitSubwayMap();

		Fubway.this.mBitmapTextureAtlasID = 1;
		this.mEngine.getTextureManager().loadTexture(this.mWelcomeBitmapTextureAtlas);
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
		
		/* Create the icons and add them to the scene. */
		scene.attachChild(new Sprite(0, 0, this.mWelcomeTextureRegion));

		scene.setOnSceneTouchListener(this);
		
		return scene;
	}

	@Override
	public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent){
		
		
		
		/* Removing entities can only be done safely on the UpdateThread.
		 * Doing it while updating/drawing can
		 * cause an exception with a suddenly missing entity.
		 * Alternatively, there is a possibility to run the TouchEvents on the UpdateThread by default, by doing:
		 * engineOptions.getTouchOptions().setRunOnUpdateThread(true);
		 * when creating the Engine in onLoadEngine(); */
		this.runOnUpdateThread(new Runnable() {

			@Override
			public void run() {
				SceneTouchEventRun(pScene, pSceneTouchEvent);
			}
		});
		return false;
	}
	

	@Override
	public void onLoadComplete() {

	}

	// ===========================================================
	// Methods
	// ===========================================================
	
	
	
	void SceneTouchEventRun(final Scene pScene, final TouchEvent pSceneTouchEvent){
		switch(this.mBitmapTextureAtlasID){
			case 1:	// welcome => map
	
				pScene.detachChildren();
	
				
				final Sprite mpaSprite = new Sprite(0, 0, Fubway.this.mMapTextureRegion); 
				
				this.mEngine.getTextureManager().loadTexture(this.mMapBitmapTextureAtlas);
			
				pScene.attachChild(mpaSprite);
	
//				mTextTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
//				FontFactory.setAssetBasePath("font/");
//				mFont = FontFactory.createFromAsset(mTextTexture, this, "Droid.ttf", FONT_SIZE, true, Color.BLACK);
//	
//				this.mEngine.getTextureManager().loadTextures(mTextTexture);
//				this.getFontManager().loadFonts(mFont);
//	
//				mChangeableText = new ChangeableText(0, 0, mFont, "XXXXX:XXXXX");
//				pScene.attachChild(mChangeableText);
//				
				this.mBitmapTextureAtlasID = 2;
				break;
				
			case 2:	// get start station
				
				this.mLocation = new StationLocation(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				if( mLocation.isInvalid()){
					break;
				}
				
				mStartStationId = this.mLocation.getId();
				
				BitmapTextureAtlas startStationBitmapTextureAtlas = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
				BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");
				TextureRegion startStationTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(startStationBitmapTextureAtlas, this, "selected_startstop.png", 0, 0);
	
				this.mEngine.getTextureManager().loadTexture(startStationBitmapTextureAtlas);
				mStartSprite = new Sprite(mLocation.getX() - startStationTextureRegion.getWidth() / 2, mLocation.getY() - startStationTextureRegion.getHeight()/2, startStationTextureRegion);
				pScene.attachChild(mStartSprite);
			
//				String content = Float.toString(pSceneTouchEvent.getX()) + ":" + Float.toString(pSceneTouchEvent.getY()); 
//				mChangeableText.setText(content);
//	
				this.mBitmapTextureAtlasID = 3;
				break;
				
			case 3:	// get target station
	
				mLocation = new StationLocation(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				if( mLocation.isInvalid()){
					break;
				}
				mTargetStationId = this.mLocation.getId();
				
				BitmapTextureAtlas targetStationBitmapTextureAtlas = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
				BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");
				TextureRegion targetStationTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(targetStationBitmapTextureAtlas, this, "selected_targetstop.png", 0, 0);
	
				this.mEngine.getTextureManager().loadTexture(targetStationBitmapTextureAtlas);
				
				mTargetSprite = new Sprite(mLocation.getX() - targetStationBitmapTextureAtlas.getWidth()/2, mLocation.getY()-targetStationBitmapTextureAtlas.getHeight()/2, targetStationTextureRegion); 
	
				pScene.attachChild(mTargetSprite);
				
				this.mBitmapTextureAtlasID = 4;
				break;
			case 4:	// new target station
	
				mLocation = new StationLocation(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
	
				if( mLocation.isGo() ){
					this.mBitmapTextureAtlasID = 5;
					pScene.detachChildren();
					InitEnter();

					final Sprite enterSprite = new Sprite(0, 0, Fubway.this.mEnterTextureRegion); 
					
					this.mEngine.getTextureManager().loadTexture(this.mEnterBitmapTextureAtlas);
				
					pScene.attachChild(enterSprite);
					
					Person person = new Person();
					person.Init(this.mEngine, this, 300, 230);
					person.BindScene(pScene);


					break;
				}
				else if( mLocation.isInvalid()){
					break;
				}
				else
				{
					this.mStartStationId = this.mTargetStationId;
					this.mTargetStationId = this.mLocation.getId();
					
					this.mStartSprite.setPosition(this.mTargetSprite.getX(), this.mTargetSprite.getY());
					this.mTargetSprite.setPosition(this.mLocation.getX() - this.mTargetSprite.getWidth()/2, this.mLocation.getY() - this.mTargetSprite.getHeight()/2);
				}
	
				
				
				break;
			case 5: // go
				break;
				
			
		}
	
	}
	
	
	private BitmapTextureAtlas NewFullBitmapTextureAtlas(){
		return new BitmapTextureAtlas(getResources().getInteger(R.integer.fullBitmapTextureAtlasSize), getResources().getInteger(R.integer.fullBitmapTextureAtlasSize), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITextureAtlasStateListener.TextureAtlasStateAdapter<IBitmapTextureAtlasSource>() {
			@Override
			public void onTextureAtlasSourceLoadExeption(final ITextureAtlas<IBitmapTextureAtlasSource> pTextureAtlas, final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final Throwable pThrowable) {
				Fubway.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(Fubway.this, "Failed loading TextureSource: " + pBitmapTextureAtlasSource.toString(), Toast.LENGTH_LONG).show();
					}
				});
			}
		});
	}
	
	public TextureRegion NewTextureRegion(BitmapTextureAtlas bitmapTextureAtlas, String mapName){
		

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath( getResources().getString(R.string.img_path));
		return BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas, this, mapName, 0, 0);
		
		
	}

	public void InitWelcome(){
		this.mWelcomeBitmapTextureAtlas = NewFullBitmapTextureAtlas();
		this.mWelcomeTextureRegion = NewTextureRegion(this.mWelcomeBitmapTextureAtlas, "welcome.png");
	}

	public void InitSubwayMap(){
		this.mMapBitmapTextureAtlas = NewFullBitmapTextureAtlas();
		this.mMapTextureRegion = NewTextureRegion(this.mMapBitmapTextureAtlas, "subway_map.png");
	}
	
	public void InitEnter(){
		this.mEnterBitmapTextureAtlas = NewFullBitmapTextureAtlas();
		this.mEnterTextureRegion = NewTextureRegion(this.mWelcomeBitmapTextureAtlas, "subwaybox_door_open.png");
		
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
